<template>
  <div class="navbar">
    <div class="left-side">
      <a-space>
        <img alt="logo" class="logo" :src="domain_logo" />

        <icon-menu-fold
          v-if="appStore.device === 'mobile'"
          style="font-size: 22px; cursor: pointer"
          @click="toggleDrawerMenu"
        />
      </a-space>
    </div>
    <ul class="right-side">
      <!--<li>-->
      <!--<a-tooltip-->
      <!--:content="-->
      <!--theme === 'light' ? '点击切换为暗黑模式' : '点击切换为亮色模式'-->
      <!--"-->
      <!--&gt;-->
      <!--<a-button-->
      <!--class="nav-btn"-->
      <!--type="outline"-->
      <!--:shape="'circle'"-->
      <!--@click="handleToggleTheme"-->
      <!--&gt;-->
      <!--<template #icon>-->
      <!--<icon-moon-fill v-if="theme === 'dark'" />-->
      <!--<icon-sun-fill v-else />-->
      <!--</template>-->
      <!--</a-button>-->
      <!--</a-tooltip>-->
      <!--</li>-->

      <!-- <li>
        <a-tooltip content="消息通知">
          <div class="message-box-trigger">
            <a-badge :count="9" dot>
              <a-button
                class="nav-btn"
                type="outline"
                :shape="'circle'"
                @click="setPopoverVisible"
              >
                <icon-notification />
              </a-button>
            </a-badge>
          </div>
        </a-tooltip>
        <a-popover
          trigger="click"
          :arrow-style="{ display: 'none' }"
          :content-style="{ padding: 0, minWidth: '400px' }"
          content-class="message-popover"
        >
          <div ref="refBtn" class="ref-btn"></div>
          <template #content>
            <message-box />
          </template>
        </a-popover>
      </li> -->
      <li>
        <a-tooltip
          :content="isFullscreen ? '点击退出全屏模式' : '点击切换全屏模式'"
        >
          <a-button
            class="nav-btn"
            type="outline"
            :shape="'circle'"
            @click="toggleFullScreen"
          >
            <template #icon>
              <icon-fullscreen-exit v-if="isFullscreen" />
              <icon-fullscreen v-else />
            </template>
          </a-button>
        </a-tooltip>
      </li>
      <li>
        <div class="org-change">
          <a-space>
            <a-button size="large" class="btn">
              <template #default>{{
                userStore.userInfo.deptName
              }}</template></a-button
            >
            <a-button size="large" @click="handleChangeApp" class="btn">
              <template #icon v-if="userStore.userInfo.isMultipleSmall">
                <icon-swap />
              </template>
              {{ userStore.userInfo.smallName }}</a-button
            >
            <a-button type="primary" @click="back">
              <!-- <template #icon>
              <icon-home />
            </template> -->
              <template #default>返回主页</template>
            </a-button>
          </a-space>
          <!-- <div class="org-name">{{ userStore.deptName }}</div> -->
        </div>
      </li>
      <!-- <li>
        <a-tooltip content="页面配置">
          <a-button
            class="nav-btn"
            type="outline"
            :shape="'circle'"
            @click="setVisible"
          >
            <template #icon>
              <icon-settings />
            </template>
          </a-button>
        </a-tooltip>
      </li> -->
      <li>
        <MessageTips :res="result.data"></MessageTips>
      </li>
      <!-- <li>
        <a-dropdown trigger="click">
          <a-avatar
            :size="32"
            :style="{ marginRight: '8px', cursor: 'pointer' }"
          >
            <img alt="avatar" :src="avatar" />
          </a-avatar>
          <template #content>
            <a-doption>
              <a-space @click="$router.push({ name: 'personal-center' })">
                <icon-user />
                <span> 个人中心 </span>
              </a-space>
            </a-doption>
            <a-doption>
              <a-space @click="$router.push({ name: 'change-password' })">
                <icon-lock />
                <span> 修改密码 </span>
              </a-space>
            </a-doption>
            <a-doption>
              <a-space @click="handleLogout">
                <icon-export />
                <span> 退出 </span>
              </a-space>
            </a-doption>
          </template>
        </a-dropdown>
      </li> -->
    </ul>
    <change-app-select
      v-model:open="open"
      @changeApp="changeApp"
    ></change-app-select>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, inject, reactive, onMounted } from 'vue';
  import { useDark, useToggle, useFullscreen } from '@vueuse/core';
  import { useAppStore, useUserStore } from '@/store';
  import useUser from '@/hooks/user';
  import changeAppSelect from './changeAppSelect.vue';
  import { getNoticePage } from '@/api/statisitics';
  import MessageTips from '../message-tips/index.vue';
  import getAssetsImages from '@/utils/assetsImages';
  import { changeSmallApp, getUserInfo } from '@/api/login';
import storage from '@/utils/storage';
    import { initSiteInfo } from '@/hooks/fetchBaseSite';

  import { useRouter, useRoute, RouteRecordRaw } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  const router = useRouter();
  const domain_logo = ref<any>(
    localStorage.getItem('manager_logo') || getAssetsImages('logo.png')
);

  const appStore = useAppStore();
  const userStore = useUserStore();
  const { logout } = useUser();
  const result = reactive({
    data: {},
  });
  const open = ref(false);
  const { isFullscreen, toggle: toggleFullScreen } = useFullscreen();

  const avatar: any = computed(() => {
    return userStore.userInfo.avatar;
  });
  const theme = computed(() => {
    return appStore.theme;
  });
  // const isDark = useDark({
  //   selector: 'body',
  //   attribute: 'arco-theme',
  //   valueDark: 'dark',
  //   valueLight: 'light',
  //   storageKey: 'arco-theme',
  //   onChanged(dark: boolean) {
  //     // overridden default behavior
  //     appStore.toggleTheme(dark);
  //   },
  // });
  // const toggleTheme = useToggle(isDark);
  // const handleToggleTheme = () => {
  //   toggleTheme();
  // };

  function handleChangeApp() {
    if (userStore.userInfo.isMultipleSmall || !userStore.userInfo.smallName) {
      open.value = true;
    }
  }
  async function changeApp(item) {
    changeSmallApp(item).then(async () => {
      const store = useUserStore();
      const storeInfo = await getUserInfo();
      store.userInfo = storeInfo.data.result.user;
      router.push('/').then(() => {
        window.location.reload();
      });
    });
  }
  function isHttp(url) {
    return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
  }
  function back() {
    userStore.getTongYiSystemUrl().then((url) => {
      if (!isHttp(url)) {
        Message.error('未获取到有效地址, 请联系管理员');
        return;
      }
      window.location.replace(url);
    });
  }

  const setVisible = () => {
    appStore.updateSettings({ globalSettings: true });
  };
  const refBtn = ref();

  const setPopoverVisible = () => {
    const event = new MouseEvent('click', {
      view: window,
      bubbles: true,
      cancelable: true,
    });
    refBtn.value.dispatchEvent(event);
  };
  const handleLogout = () => {
    logout();
  };
  const getNotice = async () => {
    const res = await getNoticePage();
    if (res.data.success) {
      result.data = res.data.result;
    }
  };
  const toggleDrawerMenu = inject('toggleDrawerMenu') as any;
  async function getUser() {
    const store = useUserStore();
    const storeInfo = await getUserInfo();
    store.userInfo = storeInfo.data.result.user;
  }

  // 菜单初始化
const init = () => {
    domain_logo.value = localStorage.getItem('manager_logo') || getAssetsImages('logo.png')
    // 读取未读消息数
    getNotice();
    getUser();
    console.log('init navbar')
    
  };
  onMounted(async () => {
    await initSiteInfo();
    init();
  });
  
</script>

<style scoped lang="less">
  .logo {
    max-width: 100px;
    max-height: 50px;
  }
  .navbar {
    display: flex;
    justify-content: space-between;
    height: 100%;
    background-color: var(--color-bg-2);
    border-bottom: 1px solid var(--color-border);
  }

  .left-side {
    display: flex;
    align-items: center;
    padding-left: 20px;
  }

  .right-side {
    display: flex;
    padding-right: 20px;
    list-style: none;
    :deep(.locale-select) {
      border-radius: 20px;
    }
    li {
      display: flex;
      align-items: center;
      padding: 0 10px;
    }

    a {
      color: var(--color-text-1);
      text-decoration: none;
    }
    .nav-btn {
      border-color: rgb(var(--gray-2));
      color: rgb(var(--gray-8));
      font-size: 16px;
    }
    .trigger-btn,
    .ref-btn {
      position: absolute;
      bottom: 14px;
    }
    .trigger-btn {
      margin-left: 14px;
    }
  }
</style>

<style lang="less">
  .message-popover {
    .arco-popover-content {
      margin-top: 0;
    }
  }
</style>
